package com.javaforever.clocksimplejee4.serviceimpl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.javaforever.clocksimplejee4.dao.PrivilegeDao;
import com.javaforever.clocksimplejee4.daoimpl.PrivilegeDaoImpl;
import com.javaforever.clocksimplejee4.database.DBConf;
import com.javaforever.clocksimplejee4.domain.Privilege;
import com.javaforever.clocksimplejee4.domain.User;
import com.javaforever.clocksimplejee4.service.PrivilegeService;
import com.javaforever.clocksimplejee4.utils.StringUtil;

public class PrivilegeServiceImpl implements PrivilegeService{
	private static PrivilegeDao privDao = new PrivilegeDaoImpl();
	
	public boolean canAccess(User user,String url) throws Exception{
		try (Connection connection = DBConf.initDB()){
			return privDao.canAccess(connection,user, url);
		}
	}
	public boolean adminAddPrivilege(Privilege privilege) throws Exception{
		try (Connection connection = DBConf.initDB()){
			return privDao.adminAddPrivilege(connection,privilege);
		}
	}
	public boolean adminDeletePrivilege(Privilege privilege) throws Exception{
		try (Connection connection = DBConf.initDB()){
			return privDao.adminDeletePrivilege(connection,privilege);
		}
	}
	public boolean adminTogglePrivilegeCanDelete(Privilege privilege) throws Exception{
		try (Connection connection = DBConf.initDB()){
			return privDao.adminTogglePrivilegeCanDelete(connection,privilege);
		}
	}
	public boolean adminTogglePrivilegeIsAdmin(Privilege privilege) throws Exception{
		try (Connection connection = DBConf.initDB()){
			return privDao.adminTogglePrivilegeIsAdmin(connection,privilege);
		}
	}
	public List<Privilege> getPrivilegeList() throws Exception{
		try (Connection connection = DBConf.initDB()){
			return privDao.getPrivilegeList(connection);
		}
	}
	public Privilege getPrivilegeByUrl(Privilege privilege) throws Exception{
		try (Connection connection = DBConf.initDB()){
			return privDao.getPrivilegeByUrl(connection,privilege);
		}
	}
	public boolean adminEditPrivilege(Privilege priv) throws Exception {
		try (Connection connection = DBConf.initDB()){
			return privDao.adminEditPrivilege(connection,priv);
		}
	}
	
	@Override
	public Set<Privilege> listMyPrivileges(boolean isAdmin) throws Exception {
		try (Connection connection = DBConf.initDB()){
			List<Privilege> alllist=privDao.getPrivilegeList(connection);
			Set<Privilege> result = new TreeSet<Privilege>();
			if (isAdmin) {
				result.addAll(alllist);
				return result;
			}else {
				for (Privilege priv : alllist) {
					boolean myIsAdmin = !StringUtil.isBlank(priv.getIsadmin())&&priv.getIsadmin().equalsIgnoreCase("Y") == true;
					if (myIsAdmin==false) {
						result.add(priv);
					}
				}
				return result;
			}
		}
	}
	
	@Override
	public Set<String> listMyPrivilegeUrls(boolean isAdmin) throws Exception {
		try (Connection connection = DBConf.initDB()){
			List<Privilege> alllist=privDao.getPrivilegeList(connection);
			Set<String> result = new TreeSet<String>();
			if (isAdmin) {
				for (Privilege priv:alllist) {
					result.add(priv.getUrl());
				}
				return result;
			}else {
				for (Privilege priv : alllist) {
					boolean myIsAdmin = !StringUtil.isBlank(priv.getIsadmin())&&priv.getIsadmin().equalsIgnoreCase("Y") == true;
					if (myIsAdmin==false) {
						result.add(priv.getUrl());
					}
				}
				return result;
			}
		}
	}
}
